package main

import (
	"context"
	"go.uber.org/zap"
	"mall-portalv2/bootstrap"
	"mall-portalv2/global"
	"mall-portalv2/internal/server/http"
	"mall-portalv2/internal/service"
	"os"
	"os/signal"
	"runtime"
	"syscall"
	"time"
)

/**
* Author: joker
* TODO: 入口文件
* Date: 2022/8/15
* Time: 上午9:49
**/

var (
	configPath string
)

func init() {
	//设置最大可用CPU
	runtime.GOMAXPROCS(runtime.NumCPU())
	bootstrap.FlagConf()
	bootstrap.InitZap()
	// conf.InitConf()
}

// 记录一下这个版本能通过测试套件
func main() {

	svc := service.New()
	defer svc.Close()
	http.New(svc)

	// Start
	global.Log.Info("Start!")

	//为了关闭
	sigCh := make(chan os.Signal)
	defer close(sigCh)
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM)
	for {
		s := <-sigCh
		global.Log.Info("get a signal", zap.String("signal", s.String()))
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			_, cancel := context.WithTimeout(context.Background(), 35*time.Second)
			defer cancel()
			global.Log.Info("comment-admin exit")
			svc.Close()
			time.Sleep(time.Second)
			os.Exit(0)
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}
